
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <title>工作队列 · RabbitMQ in Chinese</title>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.0.0-pre.14">
        <meta name="author" content="Ping <yuanta11@gmil.com>">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="[3]Publish_Subscribe.html" />
    
    
    <link rel="prev" href="[1]Hello_World.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://mr-ping.com" target="_blank" class="custom-link">瓶先生</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    RabbitMQ中文文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    RabbitMQ简介
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../description.html">
            
                <a href="../description.html">
            
                    
                    RabbitMQ能为你做些什么？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    安装
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../installation/Installing_on_Debian_Ubuntu.html">
            
                <a href="../installation/Installing_on_Debian_Ubuntu.html">
            
                    
                    在Debian及Ubuntu系统上进行安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../installation/Platforms_supported_by_RabbitMQ.html">
            
                <a href="../installation/Platforms_supported_by_RabbitMQ.html">
            
                    
                    RabbitMQ所支持的平台
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    AMQP协议
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../AMQP/AMQP_0-9-1_Model_Explained.html">
            
                <a href="../AMQP/AMQP_0-9-1_Model_Explained.html">
            
                    
                    AMQP 0.9.1 模型解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../AMQP/amqp-0-9-1-quickref.html">
            
                <a href="../AMQP/amqp-0-9-1-quickref.html">
            
                    
                    AMQP 0.9.1 快速参考指南
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    应用教程
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" >
            
                <span>
            
                    
                    Python版
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1.1" data-path="../tutorials_with_python/[1]Hello_World.html">
            
                <a href="../tutorials_with_python/[1]Hello_World.html">
            
                    
                    Hello World
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.2" data-path="../tutorials_with_python/[2]Work_Queues.html">
            
                <a href="../tutorials_with_python/[2]Work_Queues.html">
            
                    
                    工作队列
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.3" data-path="../tutorials_with_python/[3]Publish_Subscribe.html">
            
                <a href="../tutorials_with_python/[3]Publish_Subscribe.html">
            
                    
                    发布/订阅
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.4" data-path="../tutorials_with_python/[4]Routing.html">
            
                <a href="../tutorials_with_python/[4]Routing.html">
            
                    
                    路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.5" data-path="../tutorials_with_python/[5]Topics.html">
            
                <a href="../tutorials_with_python/[5]Topics.html">
            
                    
                    主题交换机
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.6" data-path="../tutorials_with_python/[6]RPC.html">
            
                <a href="../tutorials_with_python/[6]RPC.html">
            
                    
                    远程过程调用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.2" >
            
                <span>
            
                    
                    C#版
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.2.1" data-path="../tutorials_with_csharp/HelloWorld.html">
            
                <a href="../tutorials_with_csharp/HelloWorld.html">
            
                    
                    Hello World
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.2" data-path="../tutorials_with_csharp/WorkQueue.html">
            
                <a href="../tutorials_with_csharp/WorkQueue.html">
            
                    
                    工作队列
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.3" data-path="../tutorials_with_csharp/publish&subscribe.html">
            
                <a href="../tutorials_with_csharp/publish&subscribe.html">
            
                    
                    发布/订阅
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.4" data-path="../tutorials_with_csharp/routing.html">
            
                <a href="../tutorials_with_csharp/routing.html">
            
                    
                    路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.5" data-path="../tutorials_with_csharp/Topics.html">
            
                <a href="../tutorials_with_csharp/Topics.html">
            
                    
                    主题交换机
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.6" data-path="../tutorials_with_csharp/rpc.html">
            
                <a href="../tutorials_with_csharp/rpc.html">
            
                    
                    远程过程调用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.3" >
            
                <span>
            
                    
                    golang版
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.3.1" data-path="[1]Hello_World.html">
            
                <a href="[1]Hello_World.html">
            
                    
                    Hello World
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.3.2" data-path="[2]Work_Queues.html">
            
                <a href="[2]Work_Queues.html">
            
                    
                    工作队列
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3.3" data-path="[3]Publish_Subscribe.html">
            
                <a href="[3]Publish_Subscribe.html">
            
                    
                    发布/订阅
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3.4" data-path="[4]Routing.html">
            
                <a href="[4]Routing.html">
            
                    
                    路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3.5" data-path="[5]Topics.html">
            
                <a href="[5]Topics.html">
            
                    
                    主题交换机
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3.6" data-path="[6]RPC.html">
            
                <a href="[6]RPC.html">
            
                    
                    远程过程调用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >工作队列</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <blockquote>
<p>&#x539F;&#x6587;&#xFF1A;<a href="https://www.rabbitmq.com/tutorials/tutorial-two-go.html" _target="blank">Work Queues</a><br>&#x72B6;&#x6001;&#xFF1A;&#x5F85;&#x6821;&#x5BF9;<br>&#x7FFB;&#x8BD1;&#xFF1A;<a href="https://bingjian-zhu.github.io/" _target="blank">Bingjian-Zhu</a><br>&#x6821;&#x5BF9;&#xFF1A;</p>
</blockquote>
<p><img src="https://upload.wikimedia.org/wikipedia/commons/d/d0/CC-BY-SA_icon.svg" alt="CC-BY-SA"></p>
<h2 id="&#x5DE5;&#x4F5C;&#x961F;&#x5217;">&#x5DE5;&#x4F5C;&#x961F;&#x5217;</h2>
<p><strong>&#xFF08;&#x4F7F;&#x7528;Go&#x5BA2;&#x6237;&#x7AEF;&#xFF09;</strong></p>
<!--more-->
<p><img src="http://www.rabbitmq.com/img/tutorials/python-two.png" alt="">  </p>
<p>&#x5728;<a href="https://bingjian-zhu.github.io/2019/09/01/RabbitMQ%E6%95%99%E7%A8%8B%EF%BC%88%E8%AF%91%EF%BC%89-Hello-World/" _target="blank">&#x7B2C;&#x4E00;&#x7BC7;&#x6559;&#x7A0B;</a>&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x5199;&#x4E86;&#x4E00;&#x4E2A;&#x4ECE;&#x5DF2;&#x77E5;&#x961F;&#x5217;&#x4E2D;&#x53D1;&#x9001;&#x548C;&#x83B7;&#x53D6;&#x6D88;&#x606F;&#x7684;&#x7A0B;&#x5E8F;&#x3002;&#x5728;&#x8FD9;&#x7BC7;&#x6559;&#x7A0B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5DE5;&#x4F5C;&#x961F;&#x5217;&#xFF08;Work Queue&#xFF09;&#xFF0C;&#x5B83;&#x4F1A;&#x53D1;&#x9001;&#x4E00;&#x4E9B;&#x8017;&#x65F6;&#x7684;&#x4EFB;&#x52A1;&#x7ED9;&#x591A;&#x4E2A;&#x5DE5;&#x4F5C;&#x8005;&#xFF08;Worker&#xFF09;&#x3002;</p>
<p>&#x5DE5;&#x4F5C;&#x961F;&#x5217;&#xFF08;&#x53C8;&#x79F0;&#xFF1A;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x2014;&#x2014;Task Queues&#xFF09;&#x662F;&#x4E3A;&#x4E86;&#x907F;&#x514D;&#x7B49;&#x5F85;&#x4E00;&#x4E9B;&#x5360;&#x7528;&#x5927;&#x91CF;&#x8D44;&#x6E90;&#x3001;&#x65F6;&#x95F4;&#x7684;&#x64CD;&#x4F5C;&#x3002;&#x5F53;&#x6211;&#x4EEC;&#x628A;&#x4EFB;&#x52A1;&#xFF08;Task&#xFF09;&#x5F53;&#x4F5C;&#x6D88;&#x606F;&#x53D1;&#x9001;&#x5230;&#x961F;&#x5217;&#x4E2D;&#xFF0C;&#x4E00;&#x4E2A;&#x8FD0;&#x884C;&#x5728;&#x540E;&#x53F0;&#x7684;&#x5DE5;&#x4F5C;&#x8005;&#xFF08;worker&#xFF09;&#x8FDB;&#x7A0B;&#x5C31;&#x4F1A;&#x53D6;&#x51FA;&#x4EFB;&#x52A1;&#x7136;&#x540E;&#x5904;&#x7406;&#x3002;&#x5F53;&#x4F60;&#x8FD0;&#x884C;&#x591A;&#x4E2A;&#x5DE5;&#x4F5C;&#x8005;&#xFF08;workers&#xFF09;&#xFF0C;&#x4EFB;&#x52A1;&#x5C31;&#x4F1A;&#x5728;&#x5B83;&#x4EEC;&#x4E4B;&#x95F4;&#x5171;&#x4EAB;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x6982;&#x5FF5;&#x5728;&#x7F51;&#x7EDC;&#x5E94;&#x7528;&#x4E2D;&#x662F;&#x975E;&#x5E38;&#x6709;&#x7528;&#x7684;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x5728;&#x77ED;&#x6682;&#x7684;HTTP&#x8BF7;&#x6C42;&#x4E2D;&#x5904;&#x7406;&#x4E00;&#x4E9B;&#x590D;&#x6742;&#x7684;&#x4EFB;&#x52A1;&#x3002;</p>
<h2 id="&#x51C6;&#x5907;">&#x51C6;&#x5907;</h2>
<p>&#x4E4B;&#x524D;&#x7684;&#x6559;&#x7A0B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x53D1;&#x9001;&#x4E86;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x201C;Hello World!&#x201D;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x6D88;&#x606F;&#x3002;&#x73B0;&#x5728;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x53D1;&#x9001;&#x4E00;&#x4E9B;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x628A;&#x8FD9;&#x4E9B;&#x5B57;&#x7B26;&#x4E32;&#x5F53;&#x4F5C;&#x590D;&#x6742;&#x7684;&#x4EFB;&#x52A1;&#x3002;&#x6211;&#x4EEC;&#x6CA1;&#x6709;&#x771F;&#x5B9E;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x4F8B;&#x5982;&#x56FE;&#x7247;&#x7F29;&#x653E;&#x3001;pdf&#x6587;&#x4EF6;&#x8F6C;&#x6362;&#x3002;&#x6240;&#x4EE5;&#x4F7F;&#x7528;time.sleep()&#x51FD;&#x6570;&#x6765;&#x6A21;&#x62DF;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x3002;&#x6211;&#x4EEC;&#x5728;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x52A0;&#x4E0A;&#x70B9;&#x53F7;&#xFF08;.&#xFF09;&#x6765;&#x8868;&#x793A;&#x4EFB;&#x52A1;&#x7684;&#x590D;&#x6742;&#x7A0B;&#x5EA6;&#xFF0C;&#x4E00;&#x4E2A;&#x70B9;&#xFF08;.&#xFF09;&#x5C06;&#x4F1A;&#x8017;&#x65F6;1&#x79D2;&#x949F;&#x3002;&#x6BD4;&#x5982;&quot;Hello...&quot;&#x5C31;&#x4F1A;&#x8017;&#x65F6;3&#x79D2;&#x949F;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x5C06;&#x7A0D;&#x5FAE;&#x4FEE;&#x6539;&#x524D;&#x9762;&#x793A;&#x4F8B;&#x4E2D;&#x7684;<code>send.go</code>&#x4EE3;&#x7801;&#xFF0C;&#x4EE5;&#x5141;&#x8BB8;&#x4ECE;&#x547D;&#x4EE4;&#x884C;&#x53D1;&#x9001;&#x4EFB;&#x610F;&#x6D88;&#x606F;&#x3002;&#x8BE5;&#x7A0B;&#x5E8F;&#x5C06;&#x53D1;&#x9001;&#x4EFB;&#x52A1;&#x5230;&#x6211;&#x4EEC;&#x7684;&#x5DE5;&#x4F5C;&#x961F;&#x5217;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x5C06;&#x5176;&#x547D;&#x540D;&#x4E3A;<code>new_task.go</code>&#xFF1A;</p>
<pre><code class="lang-go">body := bodyFrom(os.Args)
err = ch.Publish(
  <span class="hljs-string">&quot;&quot;</span>,           <span class="hljs-comment">// exchange</span>
  q.Name,       <span class="hljs-comment">// routing key</span>
  <span class="hljs-literal">false</span>,        <span class="hljs-comment">// mandatory</span>
  <span class="hljs-literal">false</span>,
  amqp.Publishing {
    DeliveryMode: amqp.Persistent,
    ContentType:  <span class="hljs-string">&quot;text/plain&quot;</span>,
    Body:         []<span class="hljs-keyword">byte</span>(body),
  })
failOnError(err, <span class="hljs-string">&quot;Failed to publish a message&quot;</span>)
log.Printf(<span class="hljs-string">&quot; [x] Sent %s&quot;</span>, body)
</code></pre>
<p>&#x6211;&#x4EEC;&#x65E7;&#x7684;<code>receive.go</code>&#x4E5F;&#x9700;&#x8981;&#x8FDB;&#x884C;&#x4E00;&#x4E9B;&#x66F4;&#x6539;&#xFF1A;&#x5B83;&#x9700;&#x8981;&#x4E3A;&#x6D88;&#x606F;&#x4F53;&#x4E2D;&#x6BCF;&#x4E00;&#x4E2A;&#x70B9;&#x53F7;&#xFF08;.&#xFF09;&#x6A21;&#x62DF;1&#x79D2;&#x949F;&#x7684;&#x64CD;&#x4F5C;&#x3002;&#x5B83;&#x4F1A;&#x4ECE;&#x961F;&#x5217;&#x4E2D;&#x83B7;&#x53D6;&#x6D88;&#x606F;&#x5E76;&#x6267;&#x884C;&#xFF0C;&#x6211;&#x4EEC;&#x628A;&#x5B83;&#x547D;&#x540D;&#x4E3A;<code>worker.go</code>:</p>
<pre><code class="lang-go">msgs, err := ch.Consume(
  q.Name, <span class="hljs-comment">// queue</span>
  <span class="hljs-string">&quot;&quot;</span>,     <span class="hljs-comment">// consumer</span>
  <span class="hljs-literal">true</span>,   <span class="hljs-comment">// auto-ack</span>
  <span class="hljs-literal">false</span>,  <span class="hljs-comment">// exclusive</span>
  <span class="hljs-literal">false</span>,  <span class="hljs-comment">// no-local</span>
  <span class="hljs-literal">false</span>,  <span class="hljs-comment">// no-wait</span>
  <span class="hljs-literal">nil</span>,    <span class="hljs-comment">// args</span>
)
failOnError(err, <span class="hljs-string">&quot;Failed to register a consumer&quot;</span>)

forever := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">chan</span> <span class="hljs-keyword">bool</span>)

<span class="hljs-keyword">go</span> <span class="hljs-keyword">func</span>() {
  <span class="hljs-keyword">for</span> d := <span class="hljs-keyword">range</span> msgs {
    log.Printf(<span class="hljs-string">&quot;Received a message: %s&quot;</span>, d.Body)
    dot_count := bytes.Count(d.Body, []<span class="hljs-keyword">byte</span>(<span class="hljs-string">&quot;.&quot;</span>))
    t := time.Duration(dot_count)
    time.Sleep(t * time.Second)
    log.Printf(<span class="hljs-string">&quot;Done&quot;</span>)
  }
}()

log.Printf(<span class="hljs-string">&quot; [*] Waiting for messages. To exit press CTRL+C&quot;</span>)
&lt;-forever
</code></pre>
<p>&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;&#x6211;&#x4EEC;&#x7684;&#x5047;&#x4EFB;&#x52A1;&#x6A21;&#x62DF;&#x6267;&#x884C;&#x65F6;&#x95F4;&#x3002;
&#x50CF;&#x5728;&#x6559;&#x7A0B;&#x4E00;&#x4E2D;&#x90A3;&#x6837;&#x8FD0;&#x884C;&#x5B83;&#x4EEC;</p>
<pre><code class="lang-shell"># shell 1
go run worker.go
</code></pre>
<pre><code class="lang-shell"># shell 2
go run new_task.go
</code></pre>
<h2 id="&#x5FAA;&#x73AF;&#x8C03;&#x5EA6;">&#x5FAA;&#x73AF;&#x8C03;&#x5EA6;:</h2>
<p>&#x4F7F;&#x7528;&#x5DE5;&#x4F5C;&#x961F;&#x5217;&#x7684;&#x4E00;&#x4E2A;&#x597D;&#x5904;&#x5C31;&#x662F;&#x5B83;&#x80FD;&#x591F;&#x5E76;&#x884C;&#x7684;&#x5904;&#x7406;&#x961F;&#x5217;&#x3002;&#x5982;&#x679C;&#x5806;&#x79EF;&#x4E86;&#x5F88;&#x591A;&#x4EFB;&#x52A1;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x6DFB;&#x52A0;&#x66F4;&#x591A;&#x7684;&#x5DE5;&#x4F5C;&#x8005;&#xFF08;workers&#xFF09;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#xFF0C;&#x6269;&#x5C55;&#x5F88;&#x7B80;&#x5355;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x6211;&#x4EEC;&#x5148;&#x540C;&#x65F6;&#x8FD0;&#x884C;&#x4E24;&#x4E2A;<code>worker.go</code>&#xFF0C;&#x5B83;&#x4EEC;&#x90FD;&#x4F1A;&#x4ECE;&#x961F;&#x5217;&#x4E2D;&#x83B7;&#x53D6;&#x6D88;&#x606F;&#xFF0C;&#x5230;&#x5E95;&#x662F;&#x4E0D;&#x662F;&#x8FD9;&#x6837;&#x5462;&#xFF1F;&#x6211;&#x4EEC;&#x770B;&#x770B;&#x3002;</p>
<p>&#x4F60;&#x9700;&#x8981;&#x6253;&#x5F00;&#x4E09;&#x4E2A;&#x7EC8;&#x7AEF;&#xFF0C;&#x4E24;&#x4E2A;&#x7528;&#x6765;&#x8FD0;&#x884C;<code>worker.go</code>&#xFF0C;&#x8FD9;&#x4E24;&#x4E2A;&#x7EC8;&#x7AEF;&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x7684;&#x4E24;&#x4E2A;&#x6D88;&#x8D39;&#x8005;&#xFF08;consumers&#xFF09;&#x2014;&#x2014; C1 &#x548C; C2&#x3002;</p>
<pre><code class="lang-shell"># shell 1
go run worker.go
# =&gt; [*] Waiting for messages. To exit press CTRL+C
</code></pre>
<pre><code class="lang-shell"># shell 2
go run worker.go
# =&gt; [*] Waiting for messages. To exit press CTRL+C
</code></pre>
<p>&#x7B2C;&#x4E09;&#x4E2A;&#x7EC8;&#x7AEF;&#xFF0C;&#x6211;&#x4EEC;&#x7528;&#x6765;&#x53D1;&#x5E03;&#x65B0;&#x4EFB;&#x52A1;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x53D1;&#x9001;&#x4E00;&#x4E9B;&#x6D88;&#x606F;&#x7ED9;&#x6D88;&#x8D39;&#x8005;&#xFF08;consumers&#xFF09;&#xFF1A;</p>
<pre><code class="lang-shell"># shell 3
go run new_task.go First message.
go run new_task.go Second message..
go run new_task.go Third message...
go run new_task.go Fourth message....
go run new_task.go Fifth message.....
</code></pre>
<p>&#x770B;&#x770B;&#x5230;&#x5E95;&#x53D1;&#x9001;&#x4E86;&#x4EC0;&#x4E48;&#x7ED9;&#x6211;&#x4EEC;&#x7684;&#x5DE5;&#x4F5C;&#x8005;&#xFF08;workers&#xFF09;&#xFF1A;</p>
<pre><code class="lang-go"># shell <span class="hljs-number">1</span>
<span class="hljs-keyword">go</span> run worker.<span class="hljs-keyword">go</span>
# =&gt; [*] Waiting <span class="hljs-keyword">for</span> messages. To exit press CTRL+C
# =&gt; [x] Received <span class="hljs-string">&apos;First message.&apos;</span>
# =&gt; [x] Received <span class="hljs-string">&apos;Third message...&apos;</span>
# =&gt; [x] Received <span class="hljs-string">&apos;Fifth message.....&apos;</span>
</code></pre>
<pre><code class="lang-go"># shell <span class="hljs-number">2</span>
<span class="hljs-keyword">go</span> run worker.<span class="hljs-keyword">go</span>
# =&gt; [*] Waiting <span class="hljs-keyword">for</span> messages. To exit press CTRL+C
# =&gt; [x] Received <span class="hljs-string">&apos;Second message..&apos;</span>
# =&gt; [x] Received <span class="hljs-string">&apos;Fourth message....&apos;</span>
</code></pre>
<p>&#x9ED8;&#x8BA4;&#x6765;&#x8BF4;&#xFF0C;RabbitMQ&#x4F1A;&#x6309;&#x987A;&#x5E8F;&#x5F97;&#x628A;&#x6D88;&#x606F;&#x53D1;&#x9001;&#x7ED9;&#x6BCF;&#x4E2A;&#x6D88;&#x8D39;&#x8005;&#xFF08;consumer&#xFF09;&#x3002;&#x5E73;&#x5747;&#x6BCF;&#x4E2A;&#x6D88;&#x8D39;&#x8005;&#x90FD;&#x4F1A;&#x6536;&#x5230;&#x540C;&#x7B49;&#x6570;&#x91CF;&#x5F97;&#x6D88;&#x606F;&#x3002;&#x8FD9;&#x79CD;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x5F97;&#x65B9;&#x5F0F;&#x53EB;&#x505A;&#x2014;&#x2014;&#x8F6E;&#x8BE2;&#xFF08;round-robin&#xFF09;&#x3002;&#x8BD5;&#x7740;&#x6DFB;&#x52A0;&#x4E09;&#x4E2A;&#x6216;&#x66F4;&#x591A;&#x5F97;&#x5DE5;&#x4F5C;&#x8005;&#xFF08;workers&#xFF09;&#x3002;</p>
<h2 id="&#x6D88;&#x606F;&#x786E;&#x8BA4;">&#x6D88;&#x606F;&#x786E;&#x8BA4;</h2>
<p>&#x5F53;&#x5904;&#x7406;&#x4E00;&#x4E2A;&#x6BD4;&#x8F83;&#x8017;&#x65F6;&#x5F97;&#x4EFB;&#x52A1;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F60;&#x4E5F;&#x8BB8;&#x60F3;&#x77E5;&#x9053;&#x6D88;&#x8D39;&#x8005;&#xFF08;consumers&#xFF09;&#x662F;&#x5426;&#x8FD0;&#x884C;&#x5230;&#x4E00;&#x534A;&#x5C31;&#x6302;&#x6389;&#x3002;&#x5F53;&#x524D;&#x7684;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x5F53;&#x6D88;&#x606F;&#x88AB;RabbitMQ&#x53D1;&#x9001;&#x7ED9;&#x6D88;&#x8D39;&#x8005;&#xFF08;consumers&#xFF09;&#x4E4B;&#x540E;&#xFF0C;&#x9A6C;&#x4E0A;&#x5C31;&#x4F1A;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x79FB;&#x9664;&#x3002;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#xFF0C;&#x4F60;&#x53EA;&#x8981;&#x628A;&#x4E00;&#x4E2A;&#x5DE5;&#x4F5C;&#x8005;&#xFF08;worker&#xFF09;&#x505C;&#x6B62;&#xFF0C;&#x6B63;&#x5728;&#x5904;&#x7406;&#x7684;&#x6D88;&#x606F;&#x5C31;&#x4F1A;&#x4E22;&#x5931;&#x3002;&#x540C;&#x65F6;&#xFF0C;&#x6240;&#x6709;&#x53D1;&#x9001;&#x5230;&#x8FD9;&#x4E2A;&#x5DE5;&#x4F5C;&#x8005;&#x7684;&#x8FD8;&#x6CA1;&#x6709;&#x5904;&#x7406;&#x7684;&#x6D88;&#x606F;&#x90FD;&#x4F1A;&#x4E22;&#x5931;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x4E0D;&#x60F3;&#x4E22;&#x5931;&#x4EFB;&#x4F55;&#x4EFB;&#x52A1;&#x6D88;&#x606F;&#x3002;&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x5DE5;&#x4F5C;&#x8005;&#xFF08;worker&#xFF09;&#x6302;&#x6389;&#x4E86;&#xFF0C;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x4EFB;&#x52A1;&#x4F1A;&#x91CD;&#x65B0;&#x53D1;&#x9001;&#x7ED9;&#x5176;&#x4ED6;&#x7684;&#x5DE5;&#x4F5C;&#x8005;&#xFF08;worker&#xFF09;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x9632;&#x6B62;&#x6D88;&#x606F;&#x4E22;&#x5931;&#xFF0C;RabbitMQ&#x63D0;&#x4F9B;&#x4E86;&#x6D88;&#x606F;&#x54CD;&#x5E94;&#xFF08;acknowledgments&#xFF09;&#x3002;&#x6D88;&#x8D39;&#x8005;&#x4F1A;&#x901A;&#x8FC7;&#x4E00;&#x4E2A;ack&#xFF08;&#x54CD;&#x5E94;&#xFF09;&#xFF0C;&#x544A;&#x8BC9;RabbitMQ&#x5DF2;&#x7ECF;&#x6536;&#x5230;&#x5E76;&#x5904;&#x7406;&#x4E86;&#x67D0;&#x6761;&#x6D88;&#x606F;&#xFF0C;&#x7136;&#x540E;RabbitMQ&#x5C31;&#x4F1A;&#x91CA;&#x653E;&#x5E76;&#x5220;&#x9664;&#x8FD9;&#x6761;&#x6D88;&#x606F;&#x3002;</p>
<p>&#x5982;&#x679C;&#x6D88;&#x8D39;&#x8005;&#xFF08;consumer&#xFF09;&#x6302;&#x6389;&#x4E86;&#xFF0C;&#x6CA1;&#x6709;&#x53D1;&#x9001;&#x54CD;&#x5E94;&#xFF0C;RabbitMQ&#x5C31;&#x4F1A;&#x8BA4;&#x4E3A;&#x6D88;&#x606F;&#x6CA1;&#x6709;&#x88AB;&#x5B8C;&#x5168;&#x5904;&#x7406;&#xFF0C;&#x7136;&#x540E;&#x91CD;&#x65B0;&#x53D1;&#x9001;&#x7ED9;&#x5176;&#x4ED6;&#x6D88;&#x8D39;&#x8005;&#xFF08;consumer&#xFF09;&#x3002;&#x8FD9;&#x6837;&#xFF0C;&#x5373;&#x4F7F;&#x5DE5;&#x4F5C;&#x8005;&#xFF08;workers&#xFF09;&#x5076;&#x5C14;&#x7684;&#x6302;&#x6389;&#xFF0C;&#x4E5F;&#x4E0D;&#x4F1A;&#x4E22;&#x5931;&#x6D88;&#x606F;&#x3002;</p>
<p>&#x6D88;&#x606F;&#x662F;&#x6CA1;&#x6709;&#x8D85;&#x65F6;&#x8FD9;&#x4E2A;&#x6982;&#x5FF5;&#x7684;&#xFF1B;&#x5F53;&#x5DE5;&#x4F5C;&#x8005;&#x4E0E;&#x5B83;&#x65AD;&#x5F00;&#x8FDE;&#x7684;&#x65F6;&#x5019;&#xFF0C;RabbitMQ&#x4F1A;&#x91CD;&#x65B0;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x3002;&#x8FD9;&#x6837;&#x5728;&#x5904;&#x7406;&#x4E00;&#x4E2A;&#x8017;&#x65F6;&#x975E;&#x5E38;&#x957F;&#x7684;&#x6D88;&#x606F;&#x4EFB;&#x52A1;&#x7684;&#x65F6;&#x5019;&#x5C31;&#x4E0D;&#x4F1A;&#x51FA;&#x95EE;&#x9898;&#x4E86;&#x3002;</p>
<p>&#x5728;&#x672C;&#x6559;&#x7A0B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4F7F;&#x7528;&#x624B;&#x52A8;&#x6D88;&#x606F;&#x786E;&#x8BA4;&#xFF0C;&#x901A;&#x8FC7;&#x4E3A;<code>auto-ack</code>&#x53C2;&#x6570;&#x4F20;&#x9012;false&#xFF0C;&#x4E00;&#x65E6;&#x6709;&#x4EFB;&#x52A1;&#x5B8C;&#x6210;&#xFF0C;&#x4F7F;&#x7528;<code>d.Ack&#xFF08;false&#xFF09;</code>&#x5411;RabbitMQ&#x670D;&#x52A1;&#x5668;&#x53D1;&#x9001;&#x6D88;&#x8D39;&#x5B8C;&#x6210;&#x7684;&#x786E;&#x8BA4;&#xFF08;&#x8FD9;&#x4E2A;&#x786E;&#x8BA4;&#x6D88;&#x606F;&#x662F;&#x5355;&#x6B21;&#x4F20;&#x9012;&#x7684;&#xFF09;&#x3002;</p>
<pre><code class="lang-go">msgs, err := ch.Consume(
  q.Name, <span class="hljs-comment">// queue</span>
  <span class="hljs-string">&quot;&quot;</span>,     <span class="hljs-comment">// consumer</span>
  <span class="hljs-literal">false</span>,  <span class="hljs-comment">// auto-ack</span>
  <span class="hljs-literal">false</span>,  <span class="hljs-comment">// exclusive</span>
  <span class="hljs-literal">false</span>,  <span class="hljs-comment">// no-local</span>
  <span class="hljs-literal">false</span>,  <span class="hljs-comment">// no-wait</span>
  <span class="hljs-literal">nil</span>,    <span class="hljs-comment">// args</span>
)
failOnError(err, <span class="hljs-string">&quot;Failed to register a consumer&quot;</span>)

forever := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">chan</span> <span class="hljs-keyword">bool</span>)

<span class="hljs-keyword">go</span> <span class="hljs-keyword">func</span>() {
  <span class="hljs-keyword">for</span> d := <span class="hljs-keyword">range</span> msgs {
    log.Printf(<span class="hljs-string">&quot;Received a message: %s&quot;</span>, d.Body)
    dot_count := bytes.Count(d.Body, []<span class="hljs-keyword">byte</span>(<span class="hljs-string">&quot;.&quot;</span>))
    t := time.Duration(dot_count)
    time.Sleep(t * time.Second)
    log.Printf(<span class="hljs-string">&quot;Done&quot;</span>)
    d.Ack(<span class="hljs-literal">false</span>)
  }
}()

log.Printf(<span class="hljs-string">&quot; [*] Waiting for messages. To exit press CTRL+C&quot;</span>)
&lt;-forever
</code></pre>
<p>&#x8FD0;&#x884C;&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x6211;&#x4EEC;&#x53D1;&#x73B0;&#x5373;&#x4F7F;&#x4F7F;&#x7528;CTRL+C&#x6740;&#x6389;&#x4E86;&#x4E00;&#x4E2A;&#x5DE5;&#x4F5C;&#x8005;&#xFF08;worker&#xFF09;&#x8FDB;&#x7A0B;&#xFF0C;&#x6D88;&#x606F;&#x4E5F;&#x4E0D;&#x4F1A;&#x4E22;&#x5931;&#x3002;&#x5F53;&#x5DE5;&#x4F5C;&#x8005;&#xFF08;worker&#xFF09;&#x6302;&#x6389;&#x8FD9;&#x540E;&#xFF0C;&#x6240;&#x6709;&#x6CA1;&#x6709;&#x54CD;&#x5E94;&#x7684;&#x6D88;&#x606F;&#x90FD;&#x4F1A;&#x91CD;&#x65B0;&#x53D1;&#x9001;&#x3002;</p>
<blockquote>
<h4 id="&#x5FD8;&#x8BB0;&#x786E;&#x8BA4;">&#x5FD8;&#x8BB0;&#x786E;&#x8BA4;</h4>
<p>&#x5FD8;&#x8BB0;ack&#x662F;&#x4E00;&#x4E2A;&#x5E38;&#x89C1;&#x7684;&#x9519;&#x8BEF;&#x3002;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x9519;&#x8BEF;&#xFF0C;&#x4F46;&#x540E;&#x679C;&#x662F;&#x4E25;&#x91CD;&#x7684;&#x3002;&#x5F53;&#x5BA2;&#x6237;&#x7AEF;&#x9000;&#x51FA;&#x65F6;&#xFF0C;&#x6D88;&#x606F;&#x5C06;&#x88AB;&#x91CD;&#x65B0;&#x4F20;&#x9012;&#xFF08;&#x8FD9;&#x53EF;&#x80FD;&#x770B;&#x8D77;&#x6765;&#x50CF;&#x968F;&#x673A;&#x91CD;&#x65B0;&#x4F20;&#x9012;&#xFF09;&#xFF0C;&#x4F46;&#x662F;RabbitMQ&#x5C06;&#x4F1A;&#x5360;&#x7528;&#x8D8A;&#x6765;&#x8D8A;&#x591A;&#x7684;&#x5185;&#x5B58;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x65E0;&#x6CD5;&#x91CA;&#x653E;&#x4EFB;&#x4F55;&#x672A;&#x7ECF;&#x6D88;&#x606F;&#x7684;&#x6D88;&#x606F;
&#x4E3A;&#x4E86;&#x6392;&#x9664;&#x8FD9;&#x79CD;&#x9519;&#x8BEF;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>rabbitmqctl</code>&#x547D;&#x4EE4;&#xFF0C;&#x8F93;&#x51FA;<code>messages_unacknowledged</code>&#x5B57;&#x6BB5;&#xFF1A;</p>
<pre><code>  sudo rabbitmqctl list_queues name messages_ready messages_unacknowledged
</code></pre></blockquote>
<p>Windows&#x4E0A;&#x6267;&#x884C;&#xFF1A;</p>
<blockquote>
<pre><code>  rabbitmqctl.bat list_queues name messages_ready messages_unacknowledged
</code></pre></blockquote>
<h2 id="&#x6D88;&#x606F;&#x6301;&#x4E45;&#x5316;">&#x6D88;&#x606F;&#x6301;&#x4E45;&#x5316;</h2>
<p>&#x5982;&#x679C;&#x4F60;&#x6CA1;&#x6709;&#x7279;&#x610F;&#x544A;&#x8BC9;RabbitMQ&#xFF0C;&#x90A3;&#x4E48;&#x5728;&#x5B83;&#x9000;&#x51FA;&#x6216;&#x8005;&#x5D29;&#x6E83;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5C06;&#x4F1A;&#x4E22;&#x5931;&#x6240;&#x6709;&#x961F;&#x5217;&#x548C;&#x6D88;&#x606F;&#x3002;&#x4E3A;&#x4E86;&#x786E;&#x4FDD;&#x4FE1;&#x606F;&#x4E0D;&#x4F1A;&#x4E22;&#x5931;&#xFF0C;&#x6709;&#x4E24;&#x4E2A;&#x4E8B;&#x60C5;&#x662F;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#xFF1A;&#x6211;&#x4EEC;&#x5FC5;&#x987B;&#x628A;&#x201C;&#x961F;&#x5217;&#x201D;&#x548C;&#x201C;&#x6D88;&#x606F;&#x201D;&#x8BBE;&#x4E3A;&#x6301;&#x4E45;&#x5316;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x4E3A;&#x4E86;&#x4E0D;&#x8BA9;&#x961F;&#x5217;&#x6D88;&#x5931;&#xFF0C;&#x9700;&#x8981;&#x628A;&#x961F;&#x5217;&#x58F0;&#x660E;&#x4E3A;&#x6301;&#x4E45;&#x5316;&#xFF08;durable&#xFF09;&#xFF1A;</p>
<pre><code class="lang-go">q, err := ch.QueueDeclare(
  <span class="hljs-string">&quot;hello&quot;</span>,      <span class="hljs-comment">// name</span>
  <span class="hljs-literal">true</span>,         <span class="hljs-comment">// durable</span>
  <span class="hljs-literal">false</span>,        <span class="hljs-comment">// delete when unused</span>
  <span class="hljs-literal">false</span>,        <span class="hljs-comment">// exclusive</span>
  <span class="hljs-literal">false</span>,        <span class="hljs-comment">// no-wait</span>
  <span class="hljs-literal">nil</span>,          <span class="hljs-comment">// arguments</span>
)
failOnError(err, <span class="hljs-string">&quot;Failed to declare a queue&quot;</span>)
</code></pre>
<p>&#x5C3D;&#x7BA1;&#x8FD9;&#x884C;&#x4EE3;&#x7801;&#x672C;&#x8EAB;&#x662F;&#x6B63;&#x786E;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x8FBE;&#x4E0D;&#x5230;&#x6211;&#x4EEC;&#x9884;&#x671F;&#x7684;&#x7ED3;&#x679C;&#x3002;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x5B9A;&#x4E49;&#x8FC7;&#x4E00;&#x4E2A;&#x53EB;<code>hello</code>&#x7684;&#x975E;&#x6301;&#x4E45;&#x5316;&#x961F;&#x5217;&#x3002;RabbitMq&#x4E0D;&#x5141;&#x8BB8;&#x4F60;&#x4F7F;&#x7528;&#x4E0D;&#x540C;&#x7684;&#x53C2;&#x6570;&#x91CD;&#x65B0;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x961F;&#x5217;&#xFF0C;&#x5B83;&#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x3002;&#x4F46;&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x5FEB;&#x6377;&#x7684;&#x89E3;&#x51B3;&#x65B9;&#x6CD5;&#x2014;&#x2014;&#x7528;&#x4E0D;&#x540C;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x4F8B;&#x5982;<code>task_queue</code>&#x3002;</p>
<pre><code class="lang-go">q, err := ch.QueueDeclare(
  <span class="hljs-string">&quot;task_queue&quot;</span>, <span class="hljs-comment">// name</span>
  <span class="hljs-literal">true</span>,         <span class="hljs-comment">// durable</span>
  <span class="hljs-literal">false</span>,        <span class="hljs-comment">// delete when unused</span>
  <span class="hljs-literal">false</span>,        <span class="hljs-comment">// exclusive</span>
  <span class="hljs-literal">false</span>,        <span class="hljs-comment">// no-wait</span>
  <span class="hljs-literal">nil</span>,          <span class="hljs-comment">// arguments</span>
)
failOnError(err, <span class="hljs-string">&quot;Failed to declare a queue&quot;</span>)
</code></pre>
<p>&#x8FD9;&#x4E2A;<code>durable</code>&#x5FC5;&#x987B;&#x5728;&#x751F;&#x4EA7;&#x8005;&#xFF08;producer&#xFF09;&#x548C;&#x6D88;&#x8D39;&#x8005;&#xFF08;consumer&#xFF09;&#x5BF9;&#x5E94;&#x7684;&#x4EE3;&#x7801;&#x4E2D;&#x4FEE;&#x6539;&#x3002;</p>
<p>&#x6B64;&#x65F6;&#xFF0C;&#x5DF2;&#x7ECF;&#x786E;&#x4FDD;&#x5373;&#x4F7F;RabbitMQ&#x91CD;&#x65B0;&#x542F;&#x52A8;&#xFF0C;<code>task_queue</code>&#x961F;&#x5217;&#x4E5F;&#x4E0D;&#x4F1A;&#x4E22;&#x5931;&#x3002;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5C06;&#x6D88;&#x606F;&#x6807;&#x8BB0;&#x4E3A;&#x6301;&#x4E45;&#x6027; - &#x901A;&#x8FC7;&#x8BBE;&#x7F6E;<code>amqp.Publishing</code>&#x7684;<code>amqp.Persistent</code>&#x5C5E;&#x6027;&#x5B8C;&#x6210;&#x3002;</p>
<pre><code class="lang-go">err = ch.Publish(
  <span class="hljs-string">&quot;&quot;</span>,           <span class="hljs-comment">// exchange</span>
  q.Name,       <span class="hljs-comment">// routing key</span>
  <span class="hljs-literal">false</span>,        <span class="hljs-comment">// mandatory</span>
  <span class="hljs-literal">false</span>,
  amqp.Publishing {
    DeliveryMode: amqp.Persistent,
    ContentType:  <span class="hljs-string">&quot;text/plain&quot;</span>,
    Body:         []<span class="hljs-keyword">byte</span>(body),
})
</code></pre>
<blockquote>
<h4 id="&#x6CE8;&#x610F;&#xFF1A;&#x6D88;&#x606F;&#x6301;&#x4E45;&#x5316;">&#x6CE8;&#x610F;&#xFF1A;&#x6D88;&#x606F;&#x6301;&#x4E45;&#x5316;</h4>
<p>&#x5C06;&#x6D88;&#x606F;&#x8BBE;&#x4E3A;&#x6301;&#x4E45;&#x5316;&#x5E76;&#x4E0D;&#x80FD;&#x5B8C;&#x5168;&#x4FDD;&#x8BC1;&#x4E0D;&#x4F1A;&#x4E22;&#x5931;&#x3002;&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x53EA;&#x662F;&#x544A;&#x8BC9;&#x4E86;RabbitMq&#x8981;&#x628A;&#x6D88;&#x606F;&#x5B58;&#x5230;&#x786C;&#x76D8;&#xFF0C;&#x4F46;&#x4ECE;RabbitMq&#x6536;&#x5230;&#x6D88;&#x606F;&#x5230;&#x4FDD;&#x5B58;&#x4E4B;&#x95F4;&#x8FD8;&#x662F;&#x6709;&#x4E00;&#x4E2A;&#x5F88;&#x5C0F;&#x7684;&#x95F4;&#x9694;&#x65F6;&#x95F4;&#x3002;&#x56E0;&#x4E3A;RabbitMq&#x5E76;&#x4E0D;&#x662F;&#x6240;&#x6709;&#x7684;&#x6D88;&#x606F;&#x90FD;&#x4F7F;&#x7528;fsync(2)&#x2014;&#x2014;&#x5B83;&#x6709;&#x53EF;&#x80FD;&#x53EA;&#x662F;&#x4FDD;&#x5B58;&#x5230;&#x7F13;&#x5B58;&#x4E2D;&#xFF0C;&#x5E76;&#x4E0D;&#x4E00;&#x5B9A;&#x4F1A;&#x5199;&#x5230;&#x786C;&#x76D8;&#x4E2D;&#x3002;&#x5E76;&#x4E0D;&#x80FD;&#x4FDD;&#x8BC1;&#x771F;&#x6B63;&#x7684;&#x6301;&#x4E45;&#x5316;&#xFF0C;&#x4F46;&#x5DF2;&#x7ECF;&#x8DB3;&#x591F;&#x5E94;&#x4ED8;&#x6211;&#x4EEC;&#x7684;&#x7B80;&#x5355;&#x5DE5;&#x4F5C;&#x961F;&#x5217;&#x3002;&#x5982;&#x679C;&#x60A8;&#x9700;&#x8981;&#x66F4;&#x5F3A;&#x7684;&#x4FDD;&#x8BC1;&#xFF0C;&#x90A3;&#x4E48;&#x60A8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<a href="https://www.rabbitmq.com/confirms.html" _target="blank">publisher confirms.</a>&#x3002;</p>
</blockquote>
<h2 id="&#x516C;&#x5E73;&#x8C03;&#x5EA6;">&#x516C;&#x5E73;&#x8C03;&#x5EA6;</h2>
<p>&#x4F60;&#x5E94;&#x8BE5;&#x5DF2;&#x7ECF;&#x53D1;&#x73B0;&#xFF0C;&#x5B83;&#x4ECD;&#x65E7;&#x6CA1;&#x6709;&#x6309;&#x7167;&#x6211;&#x4EEC;&#x671F;&#x671B;&#x7684;&#x90A3;&#x6837;&#x8FDB;&#x884C;&#x5206;&#x53D1;&#x3002;&#x6BD4;&#x5982;&#x6709;&#x4E24;&#x4E2A;&#x5DE5;&#x4F5C;&#x8005;&#xFF08;workers&#xFF09;&#xFF0C;&#x5904;&#x7406;&#x5947;&#x6570;&#x6D88;&#x606F;&#x7684;&#x6BD4;&#x8F83;&#x7E41;&#x5FD9;&#xFF0C;&#x5904;&#x7406;&#x5076;&#x6570;&#x6D88;&#x606F;&#x7684;&#x6BD4;&#x8F83;&#x8F7B;&#x677E;&#x3002;&#x7136;&#x800C;RabbitMQ&#x5E76;&#x4E0D;&#x77E5;&#x9053;&#x8FD9;&#x4E9B;&#xFF0C;&#x5B83;&#x4ECD;&#x7136;&#x4E00;&#x5982;&#x65E2;&#x5F80;&#x7684;&#x6D3E;&#x53D1;&#x6D88;&#x606F;&#x3002;</p>
<p>&#x8FD9;&#x65F6;&#x56E0;&#x4E3A;RabbitMQ&#x53EA;&#x7BA1;&#x5206;&#x53D1;&#x8FDB;&#x5165;&#x961F;&#x5217;&#x7684;&#x6D88;&#x606F;&#xFF0C;&#x4E0D;&#x4F1A;&#x5173;&#x5FC3;&#x6709;&#x591A;&#x5C11;&#x6D88;&#x8D39;&#x8005;&#xFF08;consumer&#xFF09;&#x6CA1;&#x6709;&#x4F5C;&#x51FA;&#x54CD;&#x5E94;&#x3002;&#x5B83;&#x76F2;&#x76EE;&#x7684;&#x628A;&#x7B2C;n-th&#x6761;&#x6D88;&#x606F;&#x53D1;&#x7ED9;&#x7B2C;n-th&#x4E2A;&#x6D88;&#x8D39;&#x8005;&#x3002;</p>
<p><img src="http://www.rabbitmq.com/img/tutorials/prefetch-count.png" alt=""></p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;&#x9884;&#x53D6;&#x8BA1;&#x6570;&#x503C;&#x4E3A;1&#x3002;&#x544A;&#x8BC9;RabbitMQ&#x4E00;&#x6B21;&#x53EA;&#x5411;&#x4E00;&#x4E2A;worker&#x53D1;&#x9001;&#x4E00;&#x6761;&#x6D88;&#x606F;&#x3002;&#x6362;&#x53E5;&#x8BDD;&#x8BF4;&#xFF0C;&#x5728;&#x5904;&#x7406;&#x5E76;&#x786E;&#x8BA4;&#x524D;&#x4E00;&#x4E2A;&#x6D88;&#x606F;&#x4E4B;&#x524D;&#xFF0C;&#x4E0D;&#x8981;&#x5411;&#x5DE5;&#x4F5C;&#x4EBA;&#x5458;&#x53D1;&#x9001;&#x65B0;&#x6D88;&#x606F;&#x3002;</p>
<pre><code class="lang-go">err = ch.Qos(
  <span class="hljs-number">1</span>,     <span class="hljs-comment">// prefetch count</span>
  <span class="hljs-number">0</span>,     <span class="hljs-comment">// prefetch size</span>
  <span class="hljs-literal">false</span>, <span class="hljs-comment">// global</span>
)
failOnError(err, <span class="hljs-string">&quot;Failed to set QoS&quot;</span>)
</code></pre>
<blockquote>
<h4 id="&#x5173;&#x4E8E;&#x961F;&#x5217;&#x5927;&#x5C0F;">&#x5173;&#x4E8E;&#x961F;&#x5217;&#x5927;&#x5C0F;</h4>
<p>&#x5982;&#x679C;&#x6240;&#x6709;&#x7684;&#x5DE5;&#x4F5C;&#x8005;&#x90FD;&#x5904;&#x7406;&#x7E41;&#x5FD9;&#x72B6;&#x6001;&#xFF0C;&#x4F60;&#x7684;&#x961F;&#x5217;&#x5C31;&#x4F1A;&#x88AB;&#x586B;&#x6EE1;&#x3002;&#x4F60;&#x9700;&#x8981;&#x7559;&#x610F;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x8981;&#x4E48;&#x6DFB;&#x52A0;&#x66F4;&#x591A;&#x7684;&#x5DE5;&#x4F5C;&#x8005;&#xFF08;workers&#xFF09;&#xFF0C;&#x8981;&#x4E48;&#x4F7F;&#x7528;&#x5176;&#x4ED6;&#x7B56;&#x7565;&#x3002;</p>
</blockquote>
<h2 id="&#x6574;&#x5408;&#x4EE3;&#x7801;">&#x6574;&#x5408;&#x4EE3;&#x7801;</h2>
<p><code>new_task.go</code>&#x7684;&#x5B8C;&#x6574;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> (
        <span class="hljs-string">&quot;log&quot;</span>
        <span class="hljs-string">&quot;os&quot;</span>
        <span class="hljs-string">&quot;strings&quot;</span>

        <span class="hljs-string">&quot;github.com/streadway/amqp&quot;</span>
)

<span class="hljs-keyword">func</span> failOnError(err error, msg <span class="hljs-keyword">string</span>) {
        <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
                log.Fatalf(<span class="hljs-string">&quot;%s: %s&quot;</span>, msg, err)
        }
}

<span class="hljs-keyword">func</span> main() {
        conn, err := amqp.Dial(<span class="hljs-string">&quot;amqp://guest:guest@localhost:5672/&quot;</span>)
        failOnError(err, <span class="hljs-string">&quot;Failed to connect to RabbitMQ&quot;</span>)
        <span class="hljs-keyword">defer</span> conn.Close()

        ch, err := conn.Channel()
        failOnError(err, <span class="hljs-string">&quot;Failed to open a channel&quot;</span>)
        <span class="hljs-keyword">defer</span> ch.Close()

        q, err := ch.QueueDeclare(
                <span class="hljs-string">&quot;task_queue&quot;</span>, <span class="hljs-comment">// name</span>
                <span class="hljs-literal">true</span>,         <span class="hljs-comment">// durable</span>
                <span class="hljs-literal">false</span>,        <span class="hljs-comment">// delete when unused</span>
                <span class="hljs-literal">false</span>,        <span class="hljs-comment">// exclusive</span>
                <span class="hljs-literal">false</span>,        <span class="hljs-comment">// no-wait</span>
                <span class="hljs-literal">nil</span>,          <span class="hljs-comment">// arguments</span>
        )
        failOnError(err, <span class="hljs-string">&quot;Failed to declare a queue&quot;</span>)

        body := bodyFrom(os.Args)
        err = ch.Publish(
                <span class="hljs-string">&quot;&quot;</span>,           <span class="hljs-comment">// exchange</span>
                q.Name,       <span class="hljs-comment">// routing key</span>
                <span class="hljs-literal">false</span>,        <span class="hljs-comment">// mandatory</span>
                <span class="hljs-literal">false</span>,
                amqp.Publishing{
                        DeliveryMode: amqp.Persistent,
                        ContentType:  <span class="hljs-string">&quot;text/plain&quot;</span>,
                        Body:         []<span class="hljs-keyword">byte</span>(body),
                })
        failOnError(err, <span class="hljs-string">&quot;Failed to publish a message&quot;</span>)
        log.Printf(<span class="hljs-string">&quot; [x] Sent %s&quot;</span>, body)
}

<span class="hljs-keyword">func</span> bodyFrom(args []<span class="hljs-keyword">string</span>) <span class="hljs-keyword">string</span> {
        <span class="hljs-keyword">var</span> s <span class="hljs-keyword">string</span>
        <span class="hljs-keyword">if</span> (<span class="hljs-built_in">len</span>(args) &lt; <span class="hljs-number">2</span>) || os.Args[<span class="hljs-number">1</span>] == <span class="hljs-string">&quot;&quot;</span> {
                s = <span class="hljs-string">&quot;hello&quot;</span>
        } <span class="hljs-keyword">else</span> {
                s = strings.Join(args[<span class="hljs-number">1</span>:], <span class="hljs-string">&quot; &quot;</span>)
        }
        <span class="hljs-keyword">return</span> s
}
</code></pre>
<p>(<a href="https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/go/new_task.go" _target="blank">new_task.go</a>&#x6E90;&#x7801;)</p>
<p><code>worker.go</code>&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> (
        <span class="hljs-string">&quot;bytes&quot;</span>
        <span class="hljs-string">&quot;github.com/streadway/amqp&quot;</span>
        <span class="hljs-string">&quot;log&quot;</span>
        <span class="hljs-string">&quot;time&quot;</span>
)

<span class="hljs-keyword">func</span> failOnError(err error, msg <span class="hljs-keyword">string</span>) {
        <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
                log.Fatalf(<span class="hljs-string">&quot;%s: %s&quot;</span>, msg, err)
        }
}

<span class="hljs-keyword">func</span> main() {
        conn, err := amqp.Dial(<span class="hljs-string">&quot;amqp://guest:guest@localhost:5672/&quot;</span>)
        failOnError(err, <span class="hljs-string">&quot;Failed to connect to RabbitMQ&quot;</span>)
        <span class="hljs-keyword">defer</span> conn.Close()

        ch, err := conn.Channel()
        failOnError(err, <span class="hljs-string">&quot;Failed to open a channel&quot;</span>)
        <span class="hljs-keyword">defer</span> ch.Close()

        q, err := ch.QueueDeclare(
                <span class="hljs-string">&quot;task_queue&quot;</span>, <span class="hljs-comment">// name</span>
                <span class="hljs-literal">true</span>,         <span class="hljs-comment">// durable</span>
                <span class="hljs-literal">false</span>,        <span class="hljs-comment">// delete when unused</span>
                <span class="hljs-literal">false</span>,        <span class="hljs-comment">// exclusive</span>
                <span class="hljs-literal">false</span>,        <span class="hljs-comment">// no-wait</span>
                <span class="hljs-literal">nil</span>,          <span class="hljs-comment">// arguments</span>
        )
        failOnError(err, <span class="hljs-string">&quot;Failed to declare a queue&quot;</span>)

        err = ch.Qos(
                <span class="hljs-number">1</span>,     <span class="hljs-comment">// prefetch count</span>
                <span class="hljs-number">0</span>,     <span class="hljs-comment">// prefetch size</span>
                <span class="hljs-literal">false</span>, <span class="hljs-comment">// global</span>
        )
        failOnError(err, <span class="hljs-string">&quot;Failed to set QoS&quot;</span>)

        msgs, err := ch.Consume(
                q.Name, <span class="hljs-comment">// queue</span>
                <span class="hljs-string">&quot;&quot;</span>,     <span class="hljs-comment">// consumer</span>
                <span class="hljs-literal">false</span>,  <span class="hljs-comment">// auto-ack</span>
                <span class="hljs-literal">false</span>,  <span class="hljs-comment">// exclusive</span>
                <span class="hljs-literal">false</span>,  <span class="hljs-comment">// no-local</span>
                <span class="hljs-literal">false</span>,  <span class="hljs-comment">// no-wait</span>
                <span class="hljs-literal">nil</span>,    <span class="hljs-comment">// args</span>
        )
        failOnError(err, <span class="hljs-string">&quot;Failed to register a consumer&quot;</span>)

        forever := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">chan</span> <span class="hljs-keyword">bool</span>)

        <span class="hljs-keyword">go</span> <span class="hljs-keyword">func</span>() {
                <span class="hljs-keyword">for</span> d := <span class="hljs-keyword">range</span> msgs {
                        log.Printf(<span class="hljs-string">&quot;Received a message: %s&quot;</span>, d.Body)
                        dot_count := bytes.Count(d.Body, []<span class="hljs-keyword">byte</span>(<span class="hljs-string">&quot;.&quot;</span>))
                        t := time.Duration(dot_count)
                        time.Sleep(t * time.Second)
                        log.Printf(<span class="hljs-string">&quot;Done&quot;</span>)
                        d.Ack(<span class="hljs-literal">false</span>)
                }
        }()

        log.Printf(<span class="hljs-string">&quot; [*] Waiting for messages. To exit press CTRL+C&quot;</span>)
        &lt;-forever
}
</code></pre>
<p>(<a href="https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/go/worker.go" _target="blank">worker.go</a> &#x6E90;&#x7801;)</p>
<p>&#x4F7F;&#x7528;&#x6D88;&#x606F;&#x54CD;&#x5E94;&#x548C;prefetch_count&#x4F60;&#x5C31;&#x53EF;&#x4EE5;&#x642D;&#x5EFA;&#x8D77;&#x4E00;&#x4E2A;&#x5DE5;&#x4F5C;&#x961F;&#x5217;&#x4E86;&#x3002;&#x8FD9;&#x4E9B;&#x6301;&#x4E45;&#x5316;&#x7684;&#x9009;&#x9879;&#x4F7F;&#x5F97;&#x5728;RabbitMQ&#x91CD;&#x542F;&#x4E4B;&#x540E;&#x4ECD;&#x7136;&#x80FD;&#x591F;&#x6062;&#x590D;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x79FB;&#x6B65;&#x6559;&#x7A0B;3&#x5B66;&#x4E60;&#x5982;&#x4F55;&#x53D1;&#x9001;&#x76F8;&#x540C;&#x7684;&#x6D88;&#x606F;&#x7ED9;&#x591A;&#x4E2A;&#x6D88;&#x8D39;&#x8005;&#xFF08;consumers&#xFF09;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="[1]Hello_World.html" class="navigation navigation-prev " aria-label="Previous page: Hello World">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="[3]Publish_Subscribe.html" class="navigation navigation-next " aria-label="Next page: 发布/订阅">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"工作队列","level":"1.5.3.2","depth":3,"next":{"title":"发布/订阅","level":"1.5.3.3","depth":3,"path":"tutorials_with_golang/[3]Publish_Subscribe.md","ref":"tutorials_with_golang/[3]Publish_Subscribe.md","articles":[]},"previous":{"title":"Hello World","level":"1.5.3.1","depth":3,"path":"tutorials_with_golang/[1]Hello_World.md","ref":"tutorials_with_golang/[1]Hello_World.md","articles":[]},"dir":"ltr"},"config":{"plugins":["ga","expandable-chapters-interactive","-lunr","-search","search-plus"],"root":"./published","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"ga":{"token":"UA-120619814-1","configuration":"auto"},"expandable-chapters-interactive":{},"search-plus":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"Ping <yuanta11@gmil.com>","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"RabbitMQ in Chinese","links":{"sharing":{"all":null,"facebook":null,"google":null,"twitter":null,"weibo":null},"sidebar":{"瓶先生":"http://mr-ping.com"}},"gitbook":"=3.0.0-pre.14","description":"RabbitMQ in Chinese"},"file":{"path":"tutorials_with_golang/[2]Work_Queues.md","mtime":"2020-10-30T03:40:00.900Z","type":"markdown"},"gitbook":{"version":"3.0.0-pre.14","time":"2020-10-30T03:40:06.094Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-ga/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/buttons.js"></script>
        
    

    </body>
</html>

